<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<!DOCTYPE html>
<head>
<title>_入库预约</title>
<script type="text/javascript">
		var filterParam = {};
		var datatable_content;
		function initTable(){
			datatable_content = $("#contentTable").dataTable({
			   "bServerSide":true,
			   "bAutoWidth":false,
	           "sAjaxSource":'${ctx}/v2/preInstorageBill/page',
	           "fnServerData": callback,
	           "fnRowCallback":rowCallBack,
	           "bFilter": false,
	           "bSort": false,
	           "sZeroRecords": "查询无记录！",
	           "bProcessing": true,
	           "oLanguage": {
	               "sUrl": '${ctx}/static/v2/js/plugins/datatables/cn.txt'
	            },
	             "aoColumns":[
	             	 {"mData": null},
	             	 {"mData": null},
	             	 {"mData": null},
	             	 {"mData": "billCode"},
	             	 {"mData": "customerOrderNo"},
	             	 {"mData": "carNo"},
	             	 {"mData": "boxNum"},
	             	 {"mData": "skuNum"},
	             	 {"mData": "goodsNum"},
	             	 {"mData": null},
	             	 {"mData": null},
	             	 {"mData": "createCode"},
	             	 {"mData": "remark"},
	             	 {"mData": null},
	             ],
	             "aoColumnDefs":[
	             	{"aTargets": [1],
		              "mRender": function (data, display, row) {
		              		<c:forEach items="${customerList}" var="customer">
								if("${customer.customerCode}" == row.customerCode){
									return "${customer.customerName}";
								}
							</c:forEach>
							return "";
			           }
		            },
		            {"aTargets": [2],
		              "mRender": function (data, display, row) {
		              		<c:forEach items="${warehouseList}" var="warehouse">
								if("${warehouse.groupCode}" == row.inwarehouseCode){
									return "${warehouse.groupName}";
								}
							</c:forEach>
							return "";
			           }
		            },
		            {"aTargets": [9],
		              "mRender": function (data, display, row) {
							return row.preSendTm ? new Date(row.preSendTm.time).Format("yyyy-MM-dd") : "";
			           }
		            },
		            {"aTargets": [10],
		              "mRender": function (data, display, row) {
							return row.orderStatus == "0" ? "已预约" :
								   row.orderStatus == "1" ? "已发货" :
								   row.orderStatus == "2" ? "已点收" :
								   row.orderStatus == "3" ? "已取消" :"";
			           }
		            },
	             	{"aTargets": [13],
		              "mRender": function (data, display, row) {
		            	  	
		              		if(row.orderStatus == "0"){
								return "<button class=\"btn btn-danger\" onclick=\"deletePreInstorgaeInfo('"+row.id +"')\" type=\"button\">删除</button>"+ 
									  "<a class=\"btn btn-primary\" href=\"${ctx}/v2/preInstorageBill/queryPreInstorageInfo/"+row.id +"\"  >修改</a>";
							}else if(row.orderStatus == "1"){
								return "<a class=\"btn btn-primary\" href=\"${ctx}/v2/preInstorageBill/queryPreInstorageInfo/"+row.id +"\" >查看预约单</a>"+
									"<a class=\"btn btn-primary\"  href=\"${ctx}/instorageBill/v2_toInstorageBillDetailPage/"+row.inBillCode +"\" >查看库存</a>"+
								      "<a class=\"btn btn-primary\"   role='button' ' onclick=initPrint(this,'printPreInstorage','preInstoragePrinter','code','"+row.billCode+"')>打印预约单</a>";
							}else if(row.orderStatus == "2"){
								return "<a class=\"btn btn-primary\" href=\"${ctx}/v2/preInstorageBill/queryPreInstorageInfo/"+row.id +"\" >查看预约单</a>"+
									"<a class=\"btn btn-primary\"  href=\"${ctx}/instorageBill/v2_toInstorageBillDetailPage/"+row.inBillCode +"\" >查看库存</a>"+
								      "<a class=\"btn btn-primary\"   role='button' ' onclick=initPrint(this,'printPreInstorage','preInstoragePrinter','code','"+row.billCode+"')>打印预约单</a>";
							}
	              		return "";
			           }
		            },
	             ]
			});
		}
		function filter(item){
			filterParam[item.name] = item;
		}
		function callback(sSource, aoData, fnCallback ) {
		   	var formParam = $("#queryForm").serializeArray();
			for(var n in filterParam){
				formParam.push(filterParam[n]);
			}
		   $.ajax({ 
		       "type": "post",
		       "contentType": "application/json", 
		       "url": sSource,    
		       "dataType": "json",   
		       "data": JSON.stringify(aoData.concat(formParam)),
		       "success": fnCallback  
		   });
		}
		
		/**
		*创建了行的回调
		*nRow为当前的tr
		*/
		function rowCallBack(nRow, aData, iDisplayIndex, iDisplayIndexFull){
			var iDisplayStart = datatable_content.fnSettings()._iDisplayStart;
			$("td:eq(0)", nRow).html(iDisplayStart+iDisplayIndex+1);
		}
		
		function load(){
			if($("#queryForm").valid()){
				if (datatable_content) {
					datatable_content.fnDestroy();
				}
				initTable();
			}
		}
	$(function() {
		$("#queryForm").validate(); 
		$("#fromDate").datepicker({ format:'yyyy-mm-dd'});
		$("#toDate").datepicker({ format:'yyyy-mm-dd'});
		var now = new Date();
		var tomorrow = new Date();
		tomorrow.setDate(now.getDate()+1);
		if(!$("#fromDate").val()){
			$("#fromDate").val(now.Format("yyyy-MM-dd"));
		}
		if($("#toDate").val() == ''){
			$("#toDate").val(tomorrow.Format("yyyy-MM-dd"));
		}
		$("#addBtn").click(function(){
			location.href ="${ctx}/v2/preInstorageBill/queryPreInstorageInfo/-1";
		});
		
	});
		/*
		*删除预约信息
		*/
		function deletePreInstorgaeInfo(id){
			$.ajax({
				type : 'post',
				url : '${ctx}/v2/preInstorageBill/deletePreInstorgaeInfo',
				data : {id:id},
				dataType : 'json',
				success : function(data) {
					$("#tr_"+id).remove();
				},
				error : function() {
					alert("删除数据错误！");
				}
			});
		}
		/*
		* 查看入库详细页面
		*/
		function link2Detail(billCode){
			$.ajax({
				type : 'post',
				url  : '${ctx}/v2/preInstorageBill/queryInstorageCountItem',
				data : {billCode:billCode},
				dataType : 'json',
				success : function(data) {
					$("#detailBody").html("");
					var detailHtml = "";
					$.each(data,function(i){
						var item = data[i];
						var curCustomerName ;
						var curWarehouseCode;
						detailHtml += "<tr>";
						<c:forEach items="${customerList}" var="customer">
							if("${customer.customerCode}" == item.customerCode){
								curCustomerName = "${customer.customerName}";
							}
						</c:forEach>
						<c:forEach items="${warehouseList}" var="warehouse">
							if("${warehouse.groupCode}" == item.warehouseCode){
								curWarehouseCode = "${warehouse.groupName}";
							}
						</c:forEach>
						detailHtml += "<td>"+curCustomerName+"</td>";
						detailHtml += "<td>"+curWarehouseCode+"</td>";
						detailHtml += "<td>"+item.sku+"</td>";
						detailHtml += "<td>"+item.num+"</td>";
						detailHtml += "</tr>";
					});
					$("#detailBody").html(detailHtml);
				},
				error : function() {
					alert("查询数据错误！");
				}
			});
		}
		function exportExcel(){
			$("#queryForm").attr("action","${ctx}/v2/preInstorageBill/outexportExcel");
	        $("#queryForm").submit();
	        
      }
	  function printNow(){
		  var billCode=$("#code").val();
		  var printName=$("#preInstoragePrinter").val();
		  var page=$("#printPageNum").val();
		  $.ajax({
				type : 'post',
				url  : '${ctx}/v2/preInstorageBill/printPreInstorage',
				data : {
					billCode:billCode
				},
				async: false,
				dataType : 'json',
				success : function(data) {
					if(data.status="success"){
						var instorageBill=data.instorageBill;
						var str ="";
						var printNum=instorageBill.printNum>1 ? "C"+instorageBill.printNum:"";
						str += "<table cellpadding='0' cellspacing='0' align = 'center' border='1'>";
						str += "<thead>";
						str += "<tr><td colspan='7' align = 'center'><h3>预约单</h3></td></tr>";
						str += "<tr><td colspan='7'><font color='red'><b>"+printNum+"</b></font></td></tr>";
						str += "<tr>";
						str += "<th  colspan= '3'>客户单号:"+instorageBill.preBill.customerOrderNo+"</th ><th  colspan= '4'>单据日期:"+date2str(new Date(instorageBill.createTime.time),"yyyy-MM-d h:m:s")+"</th >";
						str += "</tr>";
						str += "</thead>";
						str += "<tbody>";
						str += "<tr>";
						str += "<td colspan= '2'>仓库:"+instorageBill.inwarehouse.groupName+"</td><td>商家:"+instorageBill.customer.customerName+"</td><td colspan= '4' align = 'left'>入库单号:"+instorageBill.billCode+"</td>";
						str += "</tr>";
						str += "<tr style='font-size:12pt'><td width='6%'>序号</td><td width='18%'>sku</td><td width='31%'>sku名称</td><td width='10%'>生产日期</td><td width='15%'>规格</td><td width='10%' >数量</td><td width='10%'>实收数量</td></tr>";
						$.each(data.instorageBillItemPreList,function(i){
							str +="<tr style='font-size:12pt' ><td>"+(i+1)+"</td><td>"+data.instorageBillItemPreList[i].skuCode+"</td><td>"+data.instorageBillItemPreList[i].skuName+"</td><td></td><td>"+data.instorageBillItemPreList[i].spec+"</td><td align = 'right'>"+data.instorageBillItemPreList[i].goodsNum+"</td>"+
								"<td></td></tr>";
						})
						str += "</tbody>";
						str += "<tfoot align = 'center'>";
						str += "<tr><td colspan= '5'></td><td align = 'right' tdata='Sum' format='#'>######</td><td></td></tr>";
						str += "<tr><td colspan= '2' align = 'left'>收货人:</td><td colspan= '2' align = 'left'>打印日期:"+date2str(new Date(),"yyyy-MM-d h:m:s")+"</td><td colspan= '3' align = 'left'>收货日期:</td></tr>";
						str += "<tr><td colspan= '2' align = 'left'>客户签字:</td><td colspan= '5' align = 'left'>签字日期:</td></tr>";
						str += "</tfoot>";
						str += "</table>";
						printTableAndBARCODE(instorageBill.billCode,printName,"A4",page,0,154,45,10,str);
					}
				},
				error : function() {
					alert("查询数据错误！");
				}
			});
		}
	  
		
</script>
</head>
<body>
<section class="content">
	<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-search"></i>
					<h3 class="box-title">搜索栏</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				<form class="form-search" id="queryForm" action="" method="post">
					<div class="form-group row">
						<div class="col-sm-4">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 商家名称： </span> 
								<select class="form-control required" id="_customerCode" name="_EQ_customerCode" onchange="ajaxAreaGroup(this,'_warehouseCode')">
									<option value=""></option>
									<c:forEach items="${customerList}" var="customer">
										<option value="${customer.customerCode}"
											<c:if test="${param._EQ_customerCode eq customer.customerCode}">selected=selected</c:if>>${customer.customerName}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 入库仓库： </span> 
								<select class="form-control span3 required" id="_warehouseCode" name="_EQ_inwarehouseCode">
									<option value=""></option>
									<c:forEach items="${warehouseList}" var="warehouse">
										<option value="${warehouse.groupCode}"
											<c:if test="${param._EQ_inwarehouseCode eq warehouse.groupCode }">selected=selected</c:if>>${warehouse.groupName}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						
						<div class="col-sm-4">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 预约入库单状态： </span> <select
									class="form-control span3" id="_orderStatus" name="_EQ_orderStatus">
									<option value=""></option>
									<option value="0" <c:if test="${param._EQ_orderStatus eq '0' }">selected=selected</c:if>>已预约</option>
									<option value="1" <c:if test="${param._EQ_orderStatus eq '1' }">selected=selected</c:if>>已发货</option>
									<option value="2" <c:if test="${param._EQ_orderStatus eq '2' }">selected=selected</c:if>>已点收</option>
									<!-- <option value="3">取消</option> -->
								</select> 
							</div>
						</div>




					</div>


					<div class=" form-group row">

						<div class="col-sm-4">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 入库日期： </span>

								<div class="input-group">
									<div class="input-group-addon">
										<i class="fa fa-calendar"></i>
									</div>
									<input type="text" class="form-control pull-right required" id="fromDate" name="_GTE_createTm" value="${param._GTE_createTm}"/>
								</div>
								
							</div>
						</div>
						<div class="col-sm-4">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 至： </span>
								<div class="input-group-addon">
									<i class="fa fa-calendar"></i>
								</div>
								<input type="text" class="form-control pull-right required" id="toDate" name="_LT_createTm" value="${param._LT_createTm}"/>
							</div>
						</div>
						<div class="col-sm-4">
							<button class="btn btn-primary" type="button" onclick="load()">搜索</button>
							<button class="btn btn-primary" type="button" id="addBtn">新增预约</button>
							<a class="btn btn-primary" type="button" onclick="exportExcel()">导出Excel</a>
						</div>
					</div>
				</form>
				</div>

			</div>
			<!-- /.box-body -->

		</div>
		<!-- /.col -->
	</div>
	<!-- /.row -->
	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
				<i class="fa fa-list"></i>
					<h3 class="box-title">查询结果</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">

					<table id="contentTable"
						class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th>序号</th>
								<th>商家</th>
								<th>仓库</th>
								<th>预约单号</th>
								<th>客户单号</th>
								<th>车牌号</th>
								<th>箱子数量</th>
								<th>SKU数量</th>
								<th>商品数量</th>
								<th>预计到货时间</th>
								<th>状态</th>
								<th>登记人</th>
								<th>备注</th>
								<th>管理</th>
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
				</div>
			</div>
			<!-- /.box -->
		</div>
		<!-- /.col -->
	</div>
	<!-- /.row -->
	<div id="detail" class="modal fade">
		<div class="modal-dialog" style="width: 60%;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
					</button>
				</div>
				<div class="modal-body">
					<div class="box box-solid box-primary">
						<div class="box-header"><span class="box-title">预约入库单点收明细</span></div>
						<div class="box-body">
							<table id="contentTable"
								class="table table-striped table-bordered table-condensed">
								<thead>
									<tr>
										<th>商家</th>
										<th>仓库</th>
										<th>SKU</th>
										<th>商品数量</th>
									</tr>
								</thead>
								<tbody id="detailBody">
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>	
		</div>
	</div>
	<!-- 打印按钮选择打印机 -->
	<div class="modal fade"  id="printPreInstorage">
		<div class="modal-dialog" >
			<div class="modal-content"  >
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"
							aria-hidden="true">
							×
						</button>
						<h3 id="myModalLabel">
		                                                        打印上架任务信息
						</h3>
						<span id="messageLable"></span>
						<input type="hidden" id="code"/>
					</div>
					<div class="modal-body">
						<div class="control-group">
							<div class="controls">
								  选择打印机:
								<select id="preInstoragePrinter" name="preInstoragePrinter" >
		
								</select>
							</div>
							<div class="controls">
								  打印张数:
								<input type="text" id="printPageNum" name="printPageNum" value="1"/>
							</div>
						</div>
					</div>
		
					<div class="modal-footer">
						<a class="btn btn-primary" data-dismiss="modal" aria-hidden="true" onclick="printNow();"> 立即打印 </a>
						<a class="btn" data-dismiss="modal" aria-hidden="true"> 关闭 </a>
					</div>
				</div>
			</div>
		</div>
</section>	
</body>
</html>